package com.tempjob.app.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author hui
 * @Date 2021/3/19 17:58
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private boolean enable;

//    @Value("${swagger.host}")
//    private String host;

//    @Bean
//    public Docket createRestApi() {
//        List<RequestParameter> pars = new ArrayList<>();
//        RequestParameterBuilder tokenPar = new RequestParameterBuilder();
//        tokenPar.name("authorization")
//                .description("认证")
//                .in(ParameterType.HEADER)
//                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING))
//                        .defaultValue(""))
//                .required(false);
//        pars.add(tokenPar.build());
//        return new Docket(DocumentationType.OAS_30)
//                .enable(enable)
//                .host(host)
//                .apiInfo(new ApiInfoBuilder()
//                        .title("打卡系统前端服务")
//                        .description("打卡系统前端服务API")
//                        .version("1.0")
//                        .contact(new Contact("hui", "", "764245287@qq.com"))
//                        .build())
//                .groupName("1.0")
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.attendance.app.controller"))
//                .paths(PathSelectors.any())
//                .build()
//                .globalRequestParameters(pars);
//    }

    @Bean("adminApi")
    public Docket controller1Apis() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("authorization")
                .description("用户token 不需要登录不填")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("AppApi")
//                .host(host)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tempjob.app.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfo(
                        "前端服务",
                        "前端服务API",
                        "1.0",
                        "",
                        new Contact("hui", "", "764245287@qq.com"),
                        "", "", Collections.emptyList()))
                .globalOperationParameters(pars)
                .enable(enable);
    }


}
